import re

PARSER_UNDEFN   = -1
PARSER_NUMBER   = 0
PARSER_LOCATE   = 1
PARSER_OPERAT   = 2

class Slicer:
    undef = (PARSER_UNDEFN, 0)
    
    def __init__(self, fileName) -> None:
        file = open(fileName, 'r')
        self.inputRaw = file.read()
        self.input = self.inputRaw.split('\n')
        self.tokens = self.makeToken(self.input)
        self.curCommandToken = []
        self.curToken = self.undef
    
    def hasMoreCommands(self):
        return self.tokens != []
    
    def hasNextToken(self):
        return self.curCommandToken != []
    
    def toNextToken(self):
        if(self.hasNextToken()):
            self.curToken = self.curCommandToken.pop(0)
        else:
            self.curToken = self.undef
        return self.curToken
    
    def toNextCommand(self):
        self.curCommandToken = self.tokens.pop(0)
        self.toNextToken()
        return self.curCommandToken
    
    def getNextToken(self):
        if(self.hasNextToken()):
            return self.curCommandToken[0]
        return self.undef
    
    regNum = r'\d+'
    regId = r'[\w_.$:][\w_.$:\d]*'
    regOp = r'[!&|\+\-/()@=;]'
    regCom = r'//.*$'
    libCom = re.compile(regCom)
    libWord = re.compile(regNum + '|' + regId + '|' + regOp)
    
    def removeComment(self, x):
        return self.libCom.sub('', x)
    
    def splitToWord(self, x):
        return self.libWord.findall(x)
    
    def isMatch(self, reg, x):
        return re.match(reg, x) != None
    
    def isNum(self, x):
        return self.isMatch(self.regNum, x)
    
    def isId(self, x):
        return self.isMatch(self.regId, x)
    
    def isOp(self, x):
        return self.isMatch(self.regOp, x)
    
    def packToken(self, x):
        if(self.isNum(x)):
            return (PARSER_NUMBER, x)
        if(self.isId(x)):
            return (PARSER_LOCATE, x)
        if(self.isOp(x)):
            return (PARSER_OPERAT, x)
        return (PARSER_UNDEFN, x)
    
    def makeTokenLine(self, line):
        return [self.packToken(i) for i in self.splitToWord(self.removeComment(line))]
    
    def makeToken(self, input):
        return [i for i in [self.makeTokenLine(j) for j in input] if(i != [])]